<?php
	header("Content-type: text/html; charset=utf-8");
	
	$_POST['page'] = $_POST['page']?$_POST['page']:1;
	$unit = 40;
	switch($_POST['action'])
	{
		case 'manage_articles':
			if($_POST['type']=="del")
			{
				if($_POST['chose'])
				{
					$_->articles->_delete('',array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items delete fail!');
				}
				die('erro:you must be chose something!');				
			}else{
				$condition = array('id'=>$_POST['id'],'cates'=>$_POST['cates'],'type'=>$_POST['type'],'title LIKE'=>$_POST['title'],'DESC'=>'addtime','LIMIT'=>(($_POST['page']-1)*$unit).','.$unit);
				$data = $_->articles->_select(array('id','title','cates'),array_filter($condition));
			}
			break;
		case 'manage_product':
			if($_POST['type']=="del")
			{
				if($_POST['chose'])
				{
					$_->product->_delete('',array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items delete fail!');
				}
				die('erro:you must be chose something!');				
			}else{
				$condition = array('id'=>$_POST['id'],'cates'=>$_POST['cates'],'type'=>$_POST['type'],'title LIKE'=>$_POST['title'],'DESC'=>'addtime','LIMIT'=>(($_POST['page']-1)*$unit).','.$unit);
				$data = $_->product->_select(array('id','title','cates'),array_filter($condition));
			}
			break;
		case 'getvalue':
			$condition = array('id'=>$_POST['id'],'cates'=>$_POST['cates'],'name LIKE'=>$_POST['title'],'DESC'=>'id','LIMIT'=>(($_POST['page']-1)*$unit).','.$unit);
			$data = $_->source->_select(array('id','name as title','cates','link','step'),array_filter($condition));
			if($data):
			foreach($data as &$val)
			{
				switch($val['cates'])
				{
					case 1:
						$val['cates']='php';
						break;
					case 2:
						$val['cates']='css';
						break;
					case 3:
						$val['cates']='javascript';
						break;
					case 4:
						$val['cates']='templates';
						break;
				}
				switch($val['step'])
				{
					case 0:
						$val['step'] = '<font color=red>已上架</font>';
						break;
					case 1:
						$val['step'] = '<font color=gray>待更新</font>';
						break;
					case 2:
						$val['step'] = '<font color=green>已更新</font>';
						break;
				}
			}
			endif;
			break;
		case 'updatesource':
			if($_POST['chose'])	
			{
				$source = $_->source->_select(array('id','link'),array('id IN'=>implode("','",$_POST['chose'])));
				foreach($source as $val)
				{
					$content = getContent($val['link']);
					if($content)
					{
						$_->source->_update(array('content'=>$content,'step'=>2),array('id'=>$val['id']))?'':die('erro:ID:'.$val['id'].' update fail!');
					}else{
						die('erro:ID:'.$val['id'].' getContent fail!');
					}
				}
				die('OK');
			}else{
				die('erro:no id post!');
			}
			break;
		case 'movesource':
			if($_POST['chose'])	
			{
				$source = $_->source->_select(array('id','name','img','content','cates','step'),array('id IN'=>implode("','",$_POST['chose'])));
				foreach($source as $val)
				{
					if($val['content'] && $val['step']!=0)
					{
						if($_POST['to']=="product")
						{
							$img = downImgs($val['img'],$val['content']);
							$p = parse_url($val['content']);
							$p = str_replace(array('/files/pic/','.jpg'), "", $p['path']);
							$content = "<dl><dd>演示：</dd><dt><a href='/preview.php?demoid=".$p."'>演示地址</a></dt><dd>下载原始模板：</dd><dt><a href='http://zjdx1.sc.chinaz.com/Files/DownLoad/".$p.".rar'>浙江电信下载</a><a href='http://fjdx.sc.chinaz.com/Files/DownLoad/".$p.".rar'>福建电信下载</a><a href='http://jxdx1.sc.chinaz.com/Files/DownLoad/".$p.".rar'>江西电信下载</a><a href='http://wtxz.sc.chinaz.com/Files/DownLoad/".$p.".rar'>中国联通下载</a><a href='http://ydxz.sc.chinaz.com/Files/DownLoad/".$p.".rar'>中国移动下载</a></dt><dd>下载CBLIT模板：</dd><dt><a href='javascript:;'>地址1</a></dt></dl>";
							$data = array('title'=>$val['name'],'faceimg'=>$img,'content'=>$content,'addtime'=>time());
						}else{
							$data = array('title'=>$val['name'],'cates'=>$val['cates'],'content'=>$val['content'],'addtime'=>time());
						}
						$_->$_POST['to']->_insert($data)?$_->source->_update(array('step'=>0),array('id'=>$val['id'])):die('erro:ID:'.$val['id'].' moved fail!');
					}else{
						die('erro:ID:'.$val['id'].' had moved!');
					}
				}
				die('OK');
			}else{
				die('erro:no id post!');
			}
			break;
		case 'cgdb':
			extract($_POST);
			if($dbname&&$dbroot)
			{
				$filename = 'db.inc.php';
				if(file_exists(ROOT.'/function/'.$filename))
				{
					$data = file(ROOT.'/function/'.$filename);
					$data[1] = "\t".'define("DBHOST","'.$dbhost.'");'."\n";
					$data[2] = "\t".'define("DBROOT","'.$dbroot.'");'."\n";
					$data[3] = "\t".'define("DBPASS","'.$dbpass.'");'."\n";
					$data[4] = "\t".'define("DBNAME","'.$dbname.'");'."\n";
				}else{
					$data = array(
						'<?php'."\n",
						"\t".'define("DBHOST","'.$dbhost.'");'."\n",
						"\t".'define("DBROOT","'.$dbroot.'");'."\n",
						"\t".'define("DBPASS","'.$dbpass.'");'."\n",
						"\t".'define("DBNAME","'.$dbname.'");'."\n",
						'?>'."\n",
					);
				}
				writeFile($filename,implode('',$data),'other','function')?die('OK'):die('erro:writefile fail!');	
			}else{
				die('erro:input must be valueable!');
			}
			break;
		case 'createtb':
			extract($_POST);
			require_once 'install.php';
			break;
		case 'message':
			$condition = array('name LIKE'=>$_POST['name'],'email'=>$_POST['email'],'addtime <'=>$_POST['stime'],'addtime >'=>$_POST['etime'],'DESC'=>'addtime');
			$data = $_->message->_select(array('id','name','email','message','addtime','step'),array_filter($condition));
			break;
		case 'msg_read':
			if($_POST['chose'])
			{
				$_->message->_update(array('step'=>1),array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items read fail!');
			}
			die('erro:you must be chose something!');
			break;
		case 'msg_dele':
			if($_POST['chose'])
			{
				$_->message->_delete('message',array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items delete fail!');
			}
			die('erro:you must be chose something!');
			break;
		case 'online':
			time()-$_COOKIE['_time'] < OUTTIME ?(cookie(array('_time'=>time()),$time = 3600) and die('online')):die('outline');
			session_start();
			$_->users->_select(array('id'),array('id'=>$_SESSION['userinfo']['id'],'logintime'=>$_SESSION['userinfo']['logintime']))?die('online'):die('outline');
			break;
		case 'users':
			$level = array('在线','离线','已禁用');
			$level = array_keys($level,$_POST['level']);
			$condition = array('level'=>$level[0],'email'=>$_POST['email'],'nickname LIKE'=>$_POST['uname'],'DESC'=>'logintime');
			$data = $_->users->_select(array('id','nickname','email','level','logintime','status'),array_filter($condition));
			break;
		case 'user_dele':
			if($_POST['chose'])
			{
				$_->users->_delete('',array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items delete fail!');
			}
			die('erro:you must be chose something!');
			break;
		case 'user_forbid':
		case 'user_open':
			$status = $_POST['action']=="user_forbid"?"2":($_POST['action']=="user_open"?"1":"");
			if($_POST['chose'])
			{
				$_->users->_update(array('status'=>$status),array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items forbid fail!');
			}
			die('erro:you must be chose something!');
			break;
		case 'cmt':
			$condition = array('content LIKE'=>$_POST['content'],'status'=>$_POST['status'],'uname LIKE'=>$_POST['name'],'DESC'=>'addtime');
			$data = $_->cmt->_select('',array_filter($condition));
			break;
		case 'cmt_dele':
			if($_POST['chose'])
			{
				$_->cmt->_delete('',array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items delete fail!');
			}
			die('erro:you must be chose something!');
			break;
		case 'cmt_pass':
		case 'cmt_unpass':
			$status = $_POST['action']=="cmt_pass"?"1":($_POST['action']=="cmt_unpass"?"2":"");
			if($_POST['chose'])
			{
				$_->cmt->_update(array('status'=>$status),array('id IN'=>implode("','",$_POST['chose'])))?die('OK'):die('erro:some items forbid fail!');
			}
			die('erro:you must be chose something!');
			break;
		case 'youban':
			$unit = 50;
			$condition = array('status'=>'1','Fname LIKE'=>$_POST['name'],'Fdesc LIKE'=>$_POST['cate'],'LIMIT'=>(($_POST['page']-1)*$unit).','.$unit);
			$data = $_->youban->_select(array('id','Fname','Fdesc','Fswffile','Fimgfile'),$condition);
			foreach($data as &$val)
			{
				$val['Fswffile'] = basename($val['Fswffile']);
				$val['Fimgfile'] = basename($val['Fimgfile']);
			}
			break;
		case 'dyouban':
			$_->youban->_update(array('status'=>'2'),array('id'=>$_POST['id']))?die('OK'):die('erro:some items update fail!');
			break;
		case 'myouban':
			$data = $_->youban->_select('',array('status'=>'2'));
			$file = ROOT.'/source';
			$dir = substr(date('Ymd'),2);
			foreach($data as $val)
			{
				$val['Fswffile'] = basename($val['Fswffile']);
				$val['Fimgfile'] = basename($val['Fimgfile']);
				if(file_exists(ROOT.'/source/station/'.$val['Fimgfile']))
				{
					copy(ROOT.'/source/station/'.$val['Fimgfile'],$file.'/img/'.$val['Fimgfile']);
				}
				if(file_exists(ROOT.'/source/station/'.$val['Fswffile']))
				{
					copy(ROOT.'/source/station/'.$val['Fswffile'],$file.'/swf/'.$val['Fswffile']);
				}
				$str .= 'insert into t_gequ (Fname,Furl,Fimg,Fflash,Fcode,Fdesc,Fswffile,Fimgfile) values ("'.$val['Fname'].'","'.$val['Furl'].'","'.$val['Fimg'].'","'.$val['Fflash'].'","'.$val['Fcode'].'","'.$val['Fdesc'].'","'.$dir.'/'.$val['Fswffile'].'","'.$dir.'/'.$val['Fimgfile'].'");'."\r\n ";
				$_->youban->_update(array('status'=>"3"),array('id'=>$val['id']))?"":die('erro:some items update fail!');
			}
			writeFile('data.txt',$str,'other','source') ? die('处理完毕 '.count($data).' 个资源，通知管理员上传到外网.'):die('erro:file write fail!');
			break;
	}

	echo json_encode($data);exit;

	function getContent($link)
	{
		$url = parse_url($link);
		switch($url['host'])
		{
			case 'www.111cn.net':
				if($con = file_get_contents($link))
				{
					preg_match_all("/<div class=\"bodyContent\">([\s\S]*)<\/div><div class=\"bottomAd\" id=\"c_ads4\">/",iconv("gb2312", "utf-8", $con), $match, PREG_PATTERN_ORDER);
					$result = $match[1][0];
				}else{
					$result = null;
				}
				break;
			case 'sc.chinaz.com':
				if($con = file_get_contents($link))
				{
					preg_match_all("/<a href=\"([^\"]*)\" title=\"(?:[^\']*)\" class=\"image_gall\">/",iconv("gb2312", "utf-8", $con), $bimg, PREG_PATTERN_ORDER);
					$result = $bimg[1][0];
				}else{
					$result = null;
				}
				break;
			default:
				$result = null;
				break;
		}
		return $result;
	}

	function downImgs($simg,$bimg)
	{
		$data['n'] = "cblit".time();
		$s = getImageSize($simg);
		$sinfo = pathinfo($simg);
		$b = getImageSize($bimg);
		$binfo = pathinfo($bimg);
		if(copy($simg,'../pic/s/'.$data['n'].'.'.$sinfo['extension']))
		{
			$data['s'] = $s[0].'*'.$s[1].'*'.$s[2]; 
		}
		if(copy($bimg,'../pic/b/'.$data['n'].'.'.$binfo['extension']))
		{
			$data['b'] = $b[0].'*'.$b[1].'*'.$b[2]; 
		}
		return serialize($data);
	}
?>